package com.xsdl.prototype;

import cn.hutool.crypto.digest.MD5;
import com.xsdl.prototype.pojo.File;

import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class Main {

    private static List<File> fileList = new CopyOnWriteArrayList<>();
    private static Map<Long, String> fileMap = new HashMap<>();

    public static void main(String[] args) throws InterruptedException {
        startAddFile();
        Thread.sleep(3000);
        startUpdateFileMap();
        Thread.sleep(5000);
    }

    // 模拟添加文件
    private static void startAddFile() {
        new Thread(() -> {
            Random random = new Random();
            while (true) {
                IntStream.rangeClosed(1, 5).forEach(i -> {
                    int randomInt = random.nextInt(10);
                    File file = fileList.stream()
                            .filter(item -> item.getId().intValue() == randomInt)
                            .findFirst().orElse(null);
                    if (file == null) {
                        fileList.add(new File()
                                .setId((long) randomInt)
                                .setName("附件" + i)
                                .setCreateTime(System.currentTimeMillis())
                                .setUpdateTime(System.currentTimeMillis()));
                    } else {
                        file.setUpdateTime(System.currentTimeMillis());
                    }
                });
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    System.out.println("线程被中断了");
                }
            }
        }).start();
    }

    // 更新文件信息并计算 MD5
    private static void startUpdateFileMap() {
        new Thread(() -> {
            boolean init = true;
            long lastUpdateTime = System.currentTimeMillis();
            while (true) {
                if (init) {
                    if (!fileList.isEmpty()) {
                        for (File file : fileList) {
                            fileMap.put(file.getId(), MD5.create().digestHex(file.toString(), StandardCharsets.UTF_8));
                        }
                    }
                    init = false;
                } else {
                    long finalLastUpdateTime = lastUpdateTime;
                    List<File> updatedFileList = fileList.stream()
                            .filter(item -> item.getUpdateTime() > finalLastUpdateTime)
                            .collect(Collectors.toList());
                    if (!updatedFileList.isEmpty()) {
                        for (File file : updatedFileList) {
                            fileMap.put(file.getId(), MD5.create().digestHex(file.toString(), StandardCharsets.UTF_8));
                            System.out.println("更新了 fileMap 中 Id 为" + file.getId() + "的数据");
                        }
                    }
                }
                lastUpdateTime = System.currentTimeMillis();
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    System.out.println("线程被中断了");
                }
            }
        }).start();
    }
}
